using System;
using System.Collections.Generic;
using System.Text;
using Xyne.UserData;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using System.IO;
using System.Xml.Serialization;
using Xyne.CardManagement;

namespace Xyne.Utilities
{
    public class UserSaveDataManager
    {
        Object stateObj;

        #region Constants
        private const string ContainerName = "XyneSaveData";
        #endregion

        #region Delegates
        public delegate void UserSaveDataLoadedDelegate(UserSaveData userSaveData);
        private UserSaveDataLoadedDelegate userSaveDataLoadedDelegate;
        #endregion

        #region Instance
        private static UserSaveDataManager instance;

        public static UserSaveDataManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new UserSaveDataManager();
                }

                return instance;
            }
        }
        #endregion

        #region Instance Variables
        private StorageDevice storageDevice;
        private UserSaveData userSaveData;
        private bool loaded;
        #endregion

        #region Properties
        public UserSaveData UserSaveData
        {
            get
            {
                if (userSaveData == null)
                {
                    GetUserSaveData();
                }
                return userSaveData;
            }
        }

        public StorageDevice UserStorageDevice
        {
            get
            {
                return storageDevice;
            }
        }

        public UserSaveDataLoadedDelegate UserSaveLoadedDelegate
        {
            get
            {
                return userSaveDataLoadedDelegate;
            }

            set
            {
                userSaveDataLoadedDelegate = value;
            }
        }

        public bool Loaded
        {
            get
            {
                return loaded;
            }

            set
            {
                loaded = value;
            }
        }
        #endregion

        #region Constructor/Init
        public UserSaveDataManager()
        {
            loaded = false;
        }
        #endregion

        #region Methods
        #region Public
        public void LoadUserSaveData()
        {
            stateObj = (object)"Get Device";
            Guide.BeginShowStorageDeviceSelector(ReceiveUserStorageDevice, stateObj);
        }

        public void SaveUserSaveData()
        {
            StorageContainer loadUserContainer = storageDevice.OpenContainer(ContainerName);

            FileStream fileStream = new FileStream(Path.Combine(loadUserContainer.Path, "usersavedata.sav"), FileMode.Create);
            XmlSerializer outputserializer = new XmlSerializer(userSaveData.GetType());

            outputserializer.Serialize(fileStream, UserSaveData);
            fileStream.Close();
            loadUserContainer.Dispose();
        }
        #endregion

        #region Private
        private void GetUserSaveData()
        {
            StorageContainer loadUserContainer = storageDevice.OpenContainer(ContainerName);
            
            if (File.Exists(Path.Combine(loadUserContainer.Path, "usersavedata.sav")))
            {
                try
                {

                    FileStream fileStream = new FileStream(Path.Combine(loadUserContainer.Path, "usersavedata.sav"), FileMode.Open);

                    userSaveData = new UserSaveData();
                    XmlSerializer xmlSerializer = new XmlSerializer(userSaveData.GetType());

                    userSaveData = xmlSerializer.Deserialize(fileStream) as UserSaveData;

                    fileStream.Close();
                }
                catch (Exception ex)
                {
                    userSaveData = new UserSaveData();


                    userSaveData.UserDeck = DeckManager.GetDeck("Fire");
                }
            }
            else
            {
                FileStream fileStream = new FileStream(Path.Combine(loadUserContainer.Path, "usersavedata.sav"), FileMode.Create);
                userSaveData = new UserSaveData();


                userSaveData.UserDeck = DeckManager.GetDeck("Fire");

                XmlSerializer outputserializer = new XmlSerializer(userSaveData.GetType());

                outputserializer.Serialize(fileStream, UserSaveData);
                fileStream.Close();
            }

            loadUserContainer.Dispose();

            return;
        }



        private void ReceiveUserStorageDevice(IAsyncResult result)
        {
            storageDevice = Guide.EndShowStorageDeviceSelector(result);
            if (storageDevice.IsConnected)
            {
                GetUserSaveData();
                Loaded = true;
                if (UserSaveLoadedDelegate != null)
                {
                    UserSaveLoadedDelegate(userSaveData);
                }
            }
        }
        #endregion
        #endregion
    }
}
